package com.example.demo_job.controller;


import com.example.demo_job.entity.*;

import com.example.demo_job.service.ManagerService;
import com.example.demo_job.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/manager")
//@CrossOrigin(origins = {"*"},methods = {RequestMethod.GET, RequestMethod.POST})
@CrossOrigin
public class ManagerController {
    @Autowired
    private ManagerService managerService;




    //还要删除txt

    //登录地址：http://localhost:8080/hqdx/manager/login
    @RequestMapping("/login")
    public R login(String mname,String mpassword){
        Manager manager=managerService.findBymname(mname);
        if(manager==null){
            //用户名不存在
            System.out.println(111);
            return R.error().message("管理员用户名不存在");

        }
        else{
            //密码错误
            Manager managerLogin = managerService.login(mname, mpassword);
            if(managerLogin==null) return R.error().message("密码错误");
            //登录成功
            return R.ok().data("items",managerLogin);
        }
    }

    //查用户地址：http://localhost:8080/hqdx/manager/showSeeker
    @RequestMapping("/showSeeker")
    public R showSeeker(){
        List<Seeker> seekerList = managerService.showSeeker();
        if(seekerList.size()==0) return R.error().message("无记录");
        return R.ok().data("items",seekerList);
    }

    //删用户地址：http://localhost:8080/hqdx/manager/deleteSeeker
    //删除用户的找工作记录，就是要删除两个表，seeker，recruit（已完成）
    @RequestMapping("/deleteSeeker")
    public R deleteSeeker(int sid){
        Seeker seeker=managerService.findBysid(sid);
        if(seeker==null) return R.error().message("删除失败");
        managerService.deleteSeeker(sid);
        return R.ok().message("删除成功");
    }



    //查公司地址：http://localhost:8080/hqdx/manager/showCompany
    @RequestMapping("/showCompany")
    public R showCompany(){
        List<Company> companyList = managerService.showCompany();
        if(companyList.size()==0) return R.error().message("无记录");
        return R.ok().data("items",companyList);
    }

    //删公司地址：http://localhost:8080/hqdx/manager/deleteCompany
    //删除三个表，company，recruit，position+txt（已完成）
    @RequestMapping("/deleteCompany")
    public R deleteCompany(int cid){
        Company company=managerService.findBycid(cid);
        if(company==null) return R.error().message("删除失败");
        managerService.deleteCompany(cid);
        return R.ok().message("删除成功");
    }

    //查职位地址：http://localhost:8080/hqdx/manager/showPosition
    @RequestMapping("/showPosition")
    public R showPosition(){
        List<ComPosition> comPositionList=managerService.showPosition();
        if(comPositionList.size()==0) return R.error().message("无记录");
        return R.ok().data("items",comPositionList);
    }

    //删职位地址：http://localhost:8080/hqdx/manager/deletePosition
    //删除两个表，position，recruit （已完成）
    @RequestMapping("/deletePosition")
    public R deletePosition(int pid){
        Position position=managerService.findBypid(pid);
        if(position==null) return R.error().message("删除失败");
        managerService.deletePosition(pid);
        managerService.deleteRecruit(pid);
        return R.ok().message("删除成功");
    }
    //查招聘地址：http://localhost:8080/hqdx/manager/showRecruit
    @RequestMapping("showRecruit")
    public R showRecruit(){
        List<RecruitSeekerComPos> recruitSeekerComPosList=managerService.showRecruit();
        if(recruitSeekerComPosList.size()==0) return R.error().message("无记录");
        return R.ok().data("items",recruitSeekerComPosList);
    }

    //删招聘地址：http://localhost:8080/hqdx/manager/deleteRecruit
    @RequestMapping("deleteRecruit")
    public R deleteRecruit(int sid,int pid){
        Recruit recruit=managerService.findRecruitBysidpid(sid,pid);
        if(recruit==null) return R.error().message("删除失败");
        //联动position表
        if(recruit.isFlag()){
            managerService.revisePositionAnumber(pid);
        }
        managerService.deleteRecruitBysidpid(sid,pid);
        return R.ok().message("删除成功");
    }
}
